package cn.lyd.base.controller;

import cn.lyd.base.dto.PhoneRegisterDto;
import cn.lyd.base.dto.SmsCodeDto;
import cn.lyd.base.service.IverifyCodeService;
import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.BussinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/verifyCode")
public class verifyCodeController {

    @Autowired
    public IverifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     * @param imageCodeKey
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey")  String imageCodeKey){
        try {
            //1.调用业务层获取图形验证码Base64字符串
            String base64Str = verifyCodeService.graph(imageCodeKey);
            //放到AjaxResult中进行返回
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BussinessException e) {
            e.printStackTrace();
            //如果是自定义异常，返回给前端的提示语是service层设置的提示语
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后重试");

        }

    }


    /**
     * 获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody  @Valid SmsCodeDto dto){
        verifyCodeService.smsCode(dto);
        return AjaxResult.me().success();
    }


    /**
     * 手机号注册
     * @param dto
     * @return
     */
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody @Valid PhoneRegisterDto dto){
        try {
            verifyCodeService.phoneRegister(dto);
            return new AjaxResult();
        } catch (BussinessException e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后再试！");
        }

    }






}
